package com.xymtop.Controller;

import com.xymtop.Annotation.Controller;
import com.xymtop.Annotation.Mapping;
import com.xymtop.Annotation.Rest;
import com.xymtop.Entity.Colloges;
import com.xymtop.Entity.Room;
import com.xymtop.Server.ResoultJson;
import com.xymtop.Service.CollogesService;
import com.xymtop.Server.Util.DBService;
import com.xymtop.Server.Util.Time;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : CollogesController
 * @Description : 院系控制器
 * @Author : 肖叶茂
 * @Date: 2022/12/13  17:31
 */
@Controller(router = "co")
public class CollogesController extends HttpServlet {
    private CollogesService collogesService = new CollogesService();

    @Rest
    @Mapping(url = "getList")
    public ResoultJson<List<Colloges>> getList(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
       List<Object> objects =  collogesService.getList();
       List<Colloges> colloges = new ArrayList<>();

       if(objects==null){
           return  new ResoultJson<>(204);
       }else {
           for(Object obj : objects){
               colloges.add((Colloges) obj);
           }
           return  new ResoultJson<>(0,colloges,"");
       }
    }

    @Rest
    @Mapping(url = "add")
    public ResoultJson<String> add(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException, IllegalAccessException {
        Colloges colloges = new Colloges();
        String name = request.getParameter("name");
        String detail = request.getParameter("detail");
        colloges.id = Time.getUnixTime();
        colloges.code = "1";
        colloges.setDetail(detail);
        colloges.setName(name);

        if(DBService.InsertObject(colloges,"colleges")){
            return  new ResoultJson<>(200);
        }else {
            return  new ResoultJson<>(204);
        }
    }

    @Rest
    @Mapping(url = "del")
    public ResoultJson<String> del(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException, IllegalAccessException {
        Colloges colloges = new Colloges();
        String id = request.getParameter("id");

        if(DBService.DelObjectById(id,"colleges")){
            return  new ResoultJson<>(200);
        }else {
            return  new ResoultJson<>(204);
        }
    }

    @Rest
    @Mapping(url = "change")
    public ResoultJson<String> change(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException, IllegalAccessException {
        Colloges colloges = new Colloges();
        String id = request.getParameter("id");
        String text = request.getParameter("text");
        String value = request.getParameter("value");

        if(DBService.execSql("UPDATE `colleges` SET "+text+" = '"+value+"' WHERE `colleges`.`id` = '"+id+"'; ")>0){
            return  new ResoultJson<>(200);
        }else {
            return  new ResoultJson<>(204);
        }
    }

    @Rest
    @Mapping(url = "getRooms")
    public ResoultJson<Object> getRooms(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException, IllegalAccessException, InstantiationException {

            List<Object> objects = DBService.Get("select * from rooms", Room.class.getCanonicalName());
        if(objects.isEmpty()){
            return  new ResoultJson<>(204);
        }else {
            return  new ResoultJson<>(0,objects,"");
        }
    }


    @Rest
    @Mapping(url = "addRoom")
    public ResoultJson<String> addRoom(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
             String name = request.getParameter("name");
             String address = request.getParameter("address");
             String detail = request.getParameter("detail");

             Room room = new Room();
             room.address = address;
             room.code = "1";
             room.id = Time.getUnixTime();
             room.name = name;
             room.detail = detail;

             if(DBService.InsertObject(room,"rooms")){
                 return  new ResoultJson<>(200);
             }else {
                 return  new ResoultJson<>(204);
             }
        }
    }
